/*******************************************************************************
 * Copyright 2011 WaTho
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.wathoserver.android.dragoid.api;

import java.util.HashMap;
import java.util.Map;

import org.jdom.Element;

/**
 * Singleton, der alle dragosien-relevanten Objekte speichert und bereitstellt.
 * 
 * @author watho
 * 
 */
public class DragoStore {

	Map<Integer, Building> buildings;
	private UserInfo userInfo = new UserInfo();

	/**
	 * Singletoninstanz
	 */
	private static DragoStore instance;

	private DragoStore() {
		buildings = new HashMap<Integer, Building>(22);
	}

	/**
	 * Liefert die Singleton-Instanz
	 * 
	 * @return
	 */
	public static DragoStore getDragoStore() {
		if (instance == null) {
			instance = new DragoStore();
		}
		return instance;
	}

	/**
	 * HashMap aller Gebäude mit key == position
	 * 
	 * @return
	 */
	public Map<Integer, Building> getBuildings() {
		return buildings;
	}

	/**
	 * Allgemeine Benutzerinfos
	 */
	public UserInfo getUserInfo() {
		return userInfo;
	}

	/**
	 * Liest die Benutzerinformationen ein.
	 * 
	 * @param ele
	 */
	public void parseUserInfo(Element ele) {
		userInfo.parseUserStatus(ele);
	}

	/**
	 * Liefert für die übergebene Position, die Position des nächsten Gebäudes.
	 * Gebäudepositionen müssen nicht fortlaufend vorhanden sein.
	 * 
	 * @param pos
	 * @return
	 */
	public int getNextBuilding(int pos) {
		pos++;
		while (!buildings.containsKey(pos)) {
			pos = ((pos + 1) % 20);
		}
		return pos;
	}

	/**
	 * Liefert für die übergebene Position, die Position des vorhergehenden
	 * Gebäudes.
	 * 
	 * @param pos
	 * @return
	 */
	public int getPreviousBuilding(int pos) {
		pos--;
		while (!buildings.containsKey(pos)) {
			pos = ((((pos - 1) % 21) + 21) % 21);
		}
		return pos;
	}
}
